//
//  BaseAnchorViewController.swift
//  SwiftDemo
//
//  Created by 李国卿 on 2020/1/16.
//  Copyright © 2020 guoqing.li. All rights reserved.
//

import UIKit

// MARK：常量
private let kItemMargin : CGFloat = 10.0
private let kHeaderViewH : CGFloat = 50.0

let kNormalItemW : CGFloat = (kScreenW - kItemMargin * 3) / 2
let kNormalItemH : CGFloat = kNormalItemW * 3 / 4
let kPrettyItemH : CGFloat = kNormalItemW * 4 / 3

private let kNormalCellID = "kNormalCellID"
private let kHeaderViewID = "kHeaderViewID"
let kPrettyCellID = "kPrettyCellID"

class BaseAnchorViewController: BaseViewController {
    // MARK: 定义属性
    var baseVM : BaseViewModel!

    lazy var collectionView : UICollectionView = {[unowned self] in
        //1. 创建布局
        let layout = UICollectionViewFlowLayout()
        //itemSize
        layout.itemSize = CGSize(width: kNormalItemW, height: kNormalItemH)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = kItemMargin
        // HeaderSize
        layout.headerReferenceSize = CGSize(width: kScreenW, height: kHeaderViewH)
        layout.sectionInset = UIEdgeInsets(top: 0, left: kItemMargin, bottom: 0, right: kItemMargin)
        //2. 创建CollectionView
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout);
        collectionView.backgroundColor = .white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.autoresizingMask = [.flexibleHeight,.flexibleWidth]
        collectionView.register(UINib(nibName: "CollectionNormalCell", bundle:nil), forCellWithReuseIdentifier:kNormalCellID )
        collectionView.register(UINib(nibName: "CollectionPrettyCell", bundle: nil), forCellWithReuseIdentifier: kPrettyCellID)
        collectionView.register(UINib(nibName: "CollectionHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: kHeaderViewID)
        return collectionView
    }()

    

    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置UI界面
        setupUI()
        // 加载数据
        loadData()
    
    }

}
// MARK : 设置 UI 界面
extension BaseAnchorViewController {
    override func setupUI() {
        // 1. 给父类 View 的引用赋值
        contentView = collectionView
        // 2. 添加 contentView
        view.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { make in
            make.edges.equalTo(0)
        }
        // 3. 再调用super
        super.setupUI()
    }
}

// MARK ： 加载数据
extension BaseAnchorViewController {
    
    @objc func  loadData(){
        
    }
}

// MARK: 遵守UICollectionViewSourceData
extension BaseAnchorViewController : UICollectionViewDataSource{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return baseVM?.anchorGroups.count ?? 0
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return baseVM?.anchorGroups[section].anchors.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier:kNormalCellID, for: indexPath) as! CollectionNormalCell
        cell.anchor = baseVM.anchorGroups[indexPath.section].anchors[indexPath.item]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: kHeaderViewID, for: indexPath) as! CollectionHeaderView;
        headerView.group = baseVM.anchorGroups[indexPath.section];
        return headerView;
    }
    
    
}

// MARK: 遵守UIColloectViewDelegate
extension BaseAnchorViewController : UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
    }
}
